package com.tiantian.quanminxiu.presenter;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.LoginView;

import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class LoginPresent implements Presenter<LoginView> {

    private LoginView loginView;

    @Override
    public void attachView(LoginView view) {
        this.loginView = view;
    }

    @Override
    public void detachView() {
        loginView = null;
    }

    public void login(String account,String pwd){
        if(account.isEmpty()||pwd.isEmpty()){
            CommonUtils.showToask(loginView.getContext(), "账号，密码不能为空");
            return;
        }
        loginView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(loginView.getContext());
        AnchorService service = app.getAnchorService();
        service.login(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE
                , account, CommonUtils.MD5(pwd))
                .flatMap(str -> {
                    return service.open(UserData.Current.mid, Constants.APP_TYPE, UserData.Current.sign
                            , UserData.Current.Equipment_Name, UserData.Current.Screen_Width, UserData.Current.Screen_Height
                            , UserData.Current.OS_Version, UserData.Current.APP_Version, UserData.Current.APP_VersionCode
                            , 116.307629, 40.058359);
                }).flatMap(str -> {
                    UserData.Current.USERCOVER_URL = str.optJSONObject("data").optString("ImgUserCover");
                    UserData.Current.USERAVATAR_URL = str.optJSONObject("data").optString("ImgUserAvatar");
                    return service.user_userinfo_info(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE);
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    loginView.Loading_Close();
                })
                .subscribe(str -> {
                    JSONObject value = str.optJSONObject("data");
                    UserData.Base.NickName = value.optString("NickName");
                    UserData.Base.User_ID = value.optInt("User_ID");
                    UserData.Base.Coin = value.optInt("Coin");
                    UserData.Base.Beans = value.optInt("Beans");
                    UserData.Base.Sex = value.optInt("Sex");
                    UserData.Base.Attention = value.optInt("Attention");
                    UserData.Base.Pomelo = value.optInt("Pomelo");
                    UserData.Base.Status = value.optInt("Status");
                    UserData.Base.Level = value.optInt("Level");
                    UserData.Base.Fans = value.optInt("Fans");
                    UserData.Base.Signature = value.optString("Signature");
                    UserData.Base.isAnchor = value.optInt("IsAnchor")==1?true:false;
                    UserData.Base.IsAdmin = value.optInt("IsAdmin")==1?true:false;
                    CommonUtils.showToask(loginView.getContext(), "登录成功");
                    loginView.JumpMain();
                }, throwable -> {
                    CommonUtils.showToask(loginView.getContext(), throwable.getMessage());
                });
    }

    public void loginQQ(String openid ,String access_token,String expires_in,String refresh_token,String nickname,String sex){
        ApplicationBase app = ApplicationBase.get(loginView.getContext());
        AnchorService service = app.getAnchorService();
        service.loginqq(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, openid
                , access_token, expires_in, refresh_token, nickname, sex)
                .flatMap(str -> {
                    return service.open(UserData.Current.mid, Constants.APP_TYPE, UserData.Current.sign
                            , UserData.Current.Equipment_Name, UserData.Current.Screen_Width, UserData.Current.Screen_Height
                            , UserData.Current.OS_Version, UserData.Current.APP_Version, UserData.Current.APP_VersionCode
                            , 116.307629, 40.058359);
                }).flatMap(str -> {
                    UserData.Current.USERCOVER_URL = str.optJSONObject("data").optString("ImgUserCover");
                    UserData.Current.USERAVATAR_URL = str.optJSONObject("data").optString("ImgUserAvatar");
                    return service.user_userinfo_info(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE);
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    loginView.Loading_Close();
                })
                .subscribe(str -> {
                    JSONObject value = str.optJSONObject("data");
                    UserData.Base.NickName = value.optString("NickName");
                    UserData.Base.User_ID = value.optInt("User_ID");
                    UserData.Base.Coin = value.optInt("Coin");
                    UserData.Base.Beans = value.optInt("Beans");
                    UserData.Base.Sex = value.optInt("Sex");
                    UserData.Base.Attention = value.optInt("Attention");
                    UserData.Base.Pomelo = value.optInt("Pomelo");
                    UserData.Base.Status = value.optInt("Status");
                    UserData.Base.Level = value.optInt("Level");
                    UserData.Base.Fans = value.optInt("Fans");
                    UserData.Base.Signature = value.optString("Signature");
                    UserData.Base.isAnchor = value.optInt("IsAnchor") == 1 ? true : false;
                    UserData.Base.IsAdmin = value.optInt("IsAdmin") == 1 ? true : false;
                    CommonUtils.showToask(loginView.getContext(), "登录成功");
                    loginView.JumpMain();
                }, throwable -> {
                    CommonUtils.showToask(loginView.getContext(), throwable.getMessage());
                });
    }
     public void loginWeixin(String openid ,String unionid, String access_token,String expires_in,String refresh_token,String nickname,String sex){
         ApplicationBase app = ApplicationBase.get(loginView.getContext());
         AnchorService service = app.getAnchorService();
         service.loginweixin(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, openid, unionid
                 , access_token, expires_in, refresh_token, nickname, sex)
                 .flatMap(str -> {
                     return service.open(UserData.Current.mid, Constants.APP_TYPE, UserData.Current.sign
                             , UserData.Current.Equipment_Name, UserData.Current.Screen_Width, UserData.Current.Screen_Height
                             , UserData.Current.OS_Version, UserData.Current.APP_Version, UserData.Current.APP_VersionCode
                             , 116.307629, 40.058359);
                 }).flatMap(str -> {
                     UserData.Current.USERCOVER_URL = str.optJSONObject("data").optString("ImgUserCover");
                     UserData.Current.USERAVATAR_URL = str.optJSONObject("data").optString("ImgUserAvatar");
                     return service.user_userinfo_info(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE);
                 })
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribeOn(app.defaultSubscribeScheduler())
                 .finallyDo(() -> {
                     loginView.Loading_Close();
                 })
                 .subscribe(str -> {
                     JSONObject value = str.optJSONObject("data");
                     UserData.Base.NickName = value.optString("NickName");
                     UserData.Base.User_ID = value.optInt("User_ID");
                     UserData.Base.Coin = value.optInt("Coin");
                     UserData.Base.Beans = value.optInt("Beans");
                     UserData.Base.Sex = value.optInt("Sex");
                     UserData.Base.Attention = value.optInt("Attention");
                     UserData.Base.Pomelo = value.optInt("Pomelo");
                     UserData.Base.Status = value.optInt("Status");
                     UserData.Base.Level = value.optInt("Level");
                     UserData.Base.Fans = value.optInt("Fans");
                     UserData.Base.Signature = value.optString("Signature");
                     UserData.Base.isAnchor = value.optInt("IsAnchor") == 1 ? true : false;
                     UserData.Base.IsAdmin = value.optInt("IsAdmin") == 1 ? true : false;
                     CommonUtils.showToask(loginView.getContext(), "登录成功");
                     loginView.JumpMain();
                 }, throwable -> {
                     CommonUtils.showToask(loginView.getContext(), throwable.getMessage());
                 });
    }
}
